﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_ContractMange : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    String CMProjectId;
    Int64 CMHospitalId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                 string _strGoodsId="";
                 int i = 0;
                 CMProjectId=SysFun.IsSQL(Request.QueryString["ProjectId"]);
                 CMHospitalId = SysFun.ToLong(_Account.AccountId);
                 if (!IsPostBack)
                 {
                     Db db = new Db();
                     System.Data.IDataReader dr;
                     dr = db.DataReader("select GoodsId from PurchaseTable where State='正常采购' and (AccountId_PS is null or AccountId_PS=0 or planpurnum=0 or planpurnum is null or PurchasePrice=0 or PurchasePrice is null) and ProjectId=" + CMProjectId + " and  AccountId_YY=" + CMHospitalId);
                     while (dr.Read())
                     {
                         _strGoodsId += dr["GoodsId"].ToString() + "、";
                         i++;
                     }
                     if (i > 0)
                     {
                         dr.Close();
                         dr.Dispose();
                         dr = null;
                         db.Close();
                         db = null;
                         Response.Write("<script>alert('您的    " + _strGoodsId.Remove(_strGoodsId.Length - 1) + "   商品没有采购价或者还没有选择\"配送企业\"和\"预采购量\"，请及时维护！系统将自动转到“维护采购目录”界面。');location.href='../Hospital/HospitalPurList.aspx?ProjectId=" + CMProjectId + "';</script>");
                         return;
                     }
                     dr.Close();
                     dr.Dispose();
                     dr = null;
                     db.Close();
                     db = null;


                 }
                 showGridView();
                 
            }
        }
    }


    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "PurchaseTable";
        dbPage.SelectFieldGroup = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";                                       //要查询的表明
        dbPage.SelectField = "UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";//要显示的字段
        dbPage.Key = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);         //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_TB,CompanyName_PS,AccountId_PS,AccountId_TB,AccountId_YY,ProjectId,print_state";//排序字段
        

        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='right' cellpadding='2' cellspacing='1'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td width='8%'>中标人注册号</td>");
        strHtml.Append("<td width='30%'>中标人名称</td>");
        strHtml.Append("<td width='8%'>配送企业注册号</td>");
        strHtml.Append("<td width='30%'>配送企业名称</td>");
        //strHtml.Append("<td  width='10%'>打印合同</td>");
        //strHtml.Append("<td  width='10%'>打印合同附表</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);

        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }

            strHtml.Append("<td width='8%'> " + dr["UserCode_TB"].ToString() + " </td>");
            if (dr["CompanyName_TB"].ToString().Trim() == "")
            {
                strHtml.Append("<td width='30%'>" + dr["CompanyName_TB"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td width='30%'><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_TB"].ToString() + "' target='_blank'>" + dr["CompanyName_TB"].ToString() + "</a></td>");
            }
            
            strHtml.Append("<td width='8%'>" + dr["UserCode_PS"].ToString() + " </td>");
            strHtml.Append("<td width='30%'><a href='../agency/company_xq.aspx?&Accountid_PS=" + dr["AccountId_PS"].ToString() + "' target='blank' target='_blank'>" + dr["CompanyName_PS"].ToString() + "</a></td>");
           // strHtml.Append("<td width='10%'><a href='Print.aspx?CompanyName_TB=" + dr["CompanyName_TB"].ToString() + "&AccountId_TB=" + dr["AccountId_TB"].ToString() + "&AccountId_PS=" + dr["AccountId_PS"].ToString() + "&CompanyName_PS=" + dr["CompanyName_PS"].ToString() + "&Projectid=" + CMProjectId + "' target='_blank' >打印合同</a>");
         //   strHtml.Append("<td width='10%'><font color= red>打印合同</font>");
            if((dr["print_state"]).ToString() != "")
            {
               
              //  strHtml.Append("<td width='10%'><a href='PrintPurList.aspx?AccountId_TB=" + dr["AccountId_TB"].ToString() + "&Accountid_PS=" + dr["AccountId_PS"].ToString() + "&ProjectId=" + CMProjectId + "' target='_blank' title='已打印'><font color= red>打印合同附表</font></a>");
            }
           else
           {
             //  strHtml.Append("<td width='10%'><a href='PrintPurList.aspx?AccountId_TB=" + dr["AccountId_TB"].ToString() + "&Accountid_PS=" + dr["AccountId_PS"].ToString() + "&ProjectId=" + CMProjectId + "' target='_blank' title='未打印'>打印合同附表</a>");
           }

         strHtml.Append("</tr>");
         }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();

        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
}


    public string fctConditions()
    {
        string strtemp = " ProjectId=" + CMProjectId + " and State='正常采购' And AccountId_YY=" + CMHospitalId + " And";

        //投标人名称
        string _strCompanyName_TB = this.txtCompanyName_TB.Text.ToString();
        if (_strCompanyName_TB != "")
        {
            strtemp += " CompanyName_TB like '%" + _strCompanyName_TB + "%' and";
        }
        //投标人编码
        string _strUserCode_TB = this.txtUserCode_TB.Text.ToString();
        if (_strUserCode_TB != "")
        {
            strtemp += " UserCode_TB like '%" + _strUserCode_TB + "%' and";
        }

        //配送企业名称
        string _strCompanyName_PS = this.txtCompanyName_PS.Text.ToString();
        if (_strCompanyName_PS != "")
        {
            strtemp += " CompanyName_PS like '%" + _strCompanyName_PS + "%' and";
        }

        //配送企业编码
        string _strUserCode_PS = this.txtUserCode_PS.Text.ToString();
        if (_strUserCode_PS != "")
        {
            strtemp += " UserCode_PS like '%" + _strUserCode_PS + "%' and";
        }

        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
      
        return strtemp;

    }

    #region TraDes 成员

    public string Name
    {
        get { return "合同管理"; }
    }

    public string Describe
    {
        get { return "";}
    }

    public string ParentName
    {
        get { return "合同管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion
}
